﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using BeautyTech.Core.Domain.Blogs;
using BeautyTech.Core.Data;
using BeautyTech.Core;

namespace BeautyTech.Services.Blogs
{
    public class BlogService : IBlogService
    {

        #region Fields

        private readonly IRepository<BlogPost> _blogPostRepository;

        #endregion

        #region Ctor
        public BlogService(IRepository<BlogPost> blogPostRepository)
        {
            this._blogPostRepository = blogPostRepository;
        }
        #endregion

        #region Methods

        /// <summary>
        /// Deletes a blog post
        /// </summary>
        /// <param name="blogPost">Blog post</param>
        public virtual void DeleteBlogPost(BlogPost blogPost)
        {
            if (blogPost == null)
                throw new ArgumentNullException("blogPost");

            _blogPostRepository.Delete(blogPost);

        }

        /// <summary>
        /// Gets a blog post
        /// </summary>
        /// <param name="blogPostId">Blog post identifier</param>
        /// <returns>Blog post</returns>
        public BlogPost GetBlogPostById(int blogPostId)
        {
            if (blogPostId == 0)
                return null;

            return _blogPostRepository.GetById(blogPostId);
        }

        /// <summary>
        /// Gets all blog posts
        /// </summary>
        /// <param name="dateFrom">Filter by created date; null if you want to get all records</param>
        /// <param name="dateTo">Filter by created date; null if you want to get all records</param>
        /// <param name="pageIndex">Page index</param>
        /// <param name="pageSize">Page size</param>
        /// <param name="showHidden">A value indicating whether to show hidden records</param>
        /// <returns>Blog posts</returns>
        public IPagedList<BlogPost> GetAllBlogPosts(DateTime? dateFrom, DateTime? dateTo, int pageIndex, int pageSize)
        {
            var query = _blogPostRepository.Table;
            if (dateFrom.HasValue)
                query = query.Where(b => dateFrom.Value <= b.CreatedOn);
            if (dateTo.HasValue)
                query = query.Where(b => dateTo.Value >= b.CreatedOn);

            query = query.OrderByDescending(b => b.CreatedOn);

            var blogPosts = new PagedList<BlogPost>(query, pageIndex, pageSize);
            return blogPosts;
        }

        /// <summary>
        /// Gets all blog posts
        /// </summary>
        public IEnumerable<BlogPost> GetAllBlogPosts()
        {
            return _blogPostRepository.Table;
        }

        /// <summary>
        /// Inserts an blog post
        /// </summary>
        /// <param name="blogPost">Blog post</param>
        public virtual void InsertBlogPost(BlogPost blogPost)
        {
            if (blogPost == null)
                throw new ArgumentNullException("blogPost");

            _blogPostRepository.Insert(blogPost);

        }

        #endregion

    }
}
